<?php
namespace Admin\Controller;

class SystemController extends BaseController{
    public function index(){
    	if (IS_POST){
    		//$this->failed('演示站，数据无法修改！');
    		$System = D('System');
    		$object = $System->form();
    		if (is_numeric($object)){
    			$output = '<?php';
    			$output .= "\r\nreturn array(";
    			$output .= "\r\n\t'WEB_NAME'=>'".str_replace("'",'"',I('post.web_name'))."',  //站点名称";
    			$output .= "\r\n\t'SESSION_KEY'=>'".str_replace("'",'"',I('post.session_key'))."',  //网站session key";
    			$output .= "\r\n\t'ADMIN_MAIL'=>'".str_replace("'",'"',I('post.admin_mail'))."',  //管理员邮箱";
    			$output .= "\r\n\t'URL_MODEL'=>'".I('post.url_model')."',  //URL模式";
    			$output .= "\r\n\t'MANAGE_ENTER'=>'".str_replace("'",'"',I('post.manage_enter'))."',  //后台入口";
    			$output .= "\r\n\t'MANAGE_SKIN'=>'".I('post.manage_skin')."',  //后台皮肤";
    			$output .= "\r\n\t'ORDER_DIR'=>'".str_replace("'",'"',I('post.order_dir'))."',  //下单页目录";
    			$output .= "\r\n\t'ORDER_DB'=>'".I('post.order_db')."',  //订单入库";
    			$output .= "\r\n\t'ORDER_TIME'=>'".I('post.order_time')."',  //防刷单间隔";
    			$output .= "\r\n\t'ORDER_SEARCH'=>'".I('post.order_search')."',  //订单查询";
    			$output .= "\r\n\t'ORDER_SEARCH_STEP'=>'".I('post.order_search_step')."',  //跨模板查询";
    			$output .= "\r\n\t'ALIPAY_SERVICE'=>'".str_replace("'",'"',I('post.alipay_service'))."',  //支付宝支付接口";
    			$output .= "\r\n\t'ALIPAY_SELLER'=>'".str_replace("'",'"',I('post.alipay_seller'))."',  //支付宝卖家账户";
    			$output .= "\r\n\t'ALIPAY_PARTNER'=>'".str_replace("'",'"',I('post.alipay_partner'))."',  //支付宝合作身份";
    			$output .= "\r\n\t'ALIPAY_KEY'=>'".str_replace("'",'"',I('post.alipay_key'))."',  //支付宝安全校验码";
    			$output .= "\r\n\t'TENPAY_PARTNER'=>'".str_replace("'",'"',I('post.tenpay_partner'))."',  //财付通商户号";
    			$output .= "\r\n\t'TENPAY_KEY'=>'".str_replace("'",'"',I('post.tenpay_key'))."',  //财付通密钥";
    			$output .= "\r\n\t'WXPAY_APPID'=>'".str_replace("'",'"',I('post.wxpay_appid'))."',  //微信支付APPID";
    			$output .= "\r\n\t'WXPAY_MCHID'=>'".str_replace("'",'"',I('post.wxpay_mchid'))."',  //微信支付MCHID";
    			$output .= "\r\n\t'WXPAY_KEY'=>'".str_replace("'",'"',I('post.wxpay_key'))."',  //微信支付KEY";
    			$output .= "\r\n\t'WXPAY_APPSECRET'=>'".str_replace("'",'"',I('post.wxpay_appsecret'))."',  //微信支付APPSECRET";
    			$output .= "\r\n\t'YUNPAY_SELLER'=>'".str_replace("'",'"',I('post.yunpay_seller'))."',  //云支付会员账户";
    			$output .= "\r\n\t'YUNPAY_PARTNER'=>'".str_replace("'",'"',I('post.yunpay_partner'))."',  //云支付合作身份";
    			$output .= "\r\n\t'YUNPAY_KEY'=>'".str_replace("'",'"',I('post.yunpay_key'))."',  //云支付安全校验码";
    			$output .= "\r\n\t'QQ_APPID'=>'".str_replace("'",'"',I('post.qq_appid'))."',  //QQ互联APP ID";
    			$output .= "\r\n\t'QQ_APPKEY'=>'".str_replace("'",'"',I('post.qq_appkey'))."',  //QQ互联APP KEY";
    			$output .= "\r\n\t'QQ_VERIFY'=>'".I('post.qq_verify')."',  //QQ互联审核模式";
    			$output .= "\r\n\t'QQ_CODE'=>'".str_replace("'",'"',I('post.qq_code',NULL,'stripslashes'))."',  //QQ互联验证代码";
    			$output .= "\r\n\t'ORDER_MAIL_SUBJECT'=>'".str_replace("'",'"',I('post.order_mail_subject'))."',  //订单提醒邮件标题";
    			$output .= "\r\n\t'ORDER_MAIL_CONTENT'=>'".str_replace("'",'"',I('post.order_mail_content',NULL,'stripslashes'))."',  //订单提醒邮件内容";
    			$output .= "\r\n\t'PAY_MAIL_SUBJECT'=>'".str_replace("'",'"',I('post.pay_mail_subject'))."',  //支付提醒邮件标题";
    			$output .= "\r\n\t'PAY_MAIL_CONTENT'=>'".str_replace("'",'"',I('pay_mail_content',NULL,'stripslashes'))."',  //支付提醒邮件内容";
    			$output .= "\r\n\t'SEND_MAIL_SUBJECT'=>'".str_replace("'",'"',I('post.send_mail_subject'))."',  //发货提醒邮件标题";
    			$output .= "\r\n\t'SEND_MAIL_CONTENT'=>'".str_replace("'",'"',I('send_mail_content',NULL,'stripslashes'))."'  //发货提醒邮件内容";
    			$output .= "\r\n);\r\n?>";
    			
    			if (file_put_contents(ROOT_PATH.'/App/Common/Conf/system.php',$output)){
    				if (I('post.manage_enter') != I('post.old_manage_enter')){
    					rename(ROOT_PATH.'/'.I('post.old_manage_enter'),ROOT_PATH.'/'.I('post.manage_enter')) ? $this->success(C('WEB_URL').I('post.manage_enter'),'后台入口修改成功，即将跳转到新入口！',0,1) : $this->failed('后台入口重命名失败，请检查系统根目录权限！');
    				}else{
    					$this->success(C('PREV_URL'),'系统设置更新成功！');
    				}
    			}else{
    				$this->failed('系统设置更新失败，请检查App/Common/Conf目录权限！');
    			}
    		}else{
    			$this->failed($object);
    		}
    	}
		$this->skin(C('MANAGE_SKIN'));
    	$this->display();
    }
    
    public function sessionKey(){
		echo getKey(40);
    }
    
    public function qqcode(){
    	$file = ROOT_PATH.'/index.html';
    	if (!is_file($file)){
    		file_put_contents($file,C('QQ_CODE')) ? $this->success(C('PREV_URL'),'验证文件生成成功！') : $this->failed('验证文件生成失败，请检查系统根目录权限！');
    	}else{
    		$this->failed('系统检测到您的系统根目录下已经存在index.html，无法生成验证文件，请备份并删除此文件，再进行生成！');
    	}
    }
    
    private function skin($n=0){
    	$html = '';
    	foreach (array('default'=>'黑色','blue'=>'蓝色','green'=>'绿色','red'=>'红色','yellow'=>'黄色','orange'=>'橙色') as $key=>$value){
    		$html .= '<option value="'.$key.'"'.($key==$n ? ' selected' : '').'>'.$value.'</option>';
    	}
    	$this->assign('Skin',$html);
    }
}